package com.g2room.myuall.grap.univ;

import java.io.IOException;
import java.net.MalformedURLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.g2room.myuall.grap.ky.model.po.GrapErr;
import com.g2room.myuall.grap.univ.po.UnivBeauty;
import com.g2room.myuall.grap.univ.po.UnivBeautyImgs;
import com.g2room.myuall.tools.ImageUtil;
import com.g2room.myuall.util.DbUtil;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;

public class GrapXiaohuaData implements Runnable {
	static PreparedStatement  ps = null;
	static String baseUrl = "http://www.xiaohuar.com";
	public static void main(String[] args) {
		GrapXiaohuaData gxd = new  GrapXiaohuaData();
		for(int i = 0; i < 42; i++) {
			gxd.getXiaohuaList("http://www.xiaohuar.com/list-1-"+i+".html");
		}
	}
	
	/**
	 * 获取分页列表数据
	 * @param url
	 * @throws FailingHttpStatusCodeException
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public void getXiaohuaList(String url) {
		System.out.println("start get page data!" + url);
		Document doc = null;
		try {
			doc = GenericGrap.getDocByIp(url);
		} catch (Exception e) {
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("1");
			ge.setDescription("获取List错误："+e.getMessage());
			insert2ErrDb(ge);
		}
        
		List<UnivBeauty> ubs = new ArrayList<UnivBeauty>();
		Elements els = doc.select("#list_img .demo .item_list .item .item_t .img a");
		if(null != els && !els.isEmpty()) {
			for(Element el : els) {
				UnivBeauty ub = new UnivBeauty();
				ub.setSourceUrl((el.attr("href").startsWith("http"))?el.attr("href") : baseUrl + el.attr("href"));
				Elements tempEls = el.getElementsByTag("img");
				if(null != tempEls && !tempEls.isEmpty()) {
					String src = tempEls.first().attr("src");
					ub.setCover(src.startsWith("http")?src : baseUrl + src);
					// 获取宽度高度
					Map<Integer, Integer> map = ImageUtil.getImgWidth(ub.getCover());
					if(null != map) {
						for(int key : map.keySet()) {
							ub.setCoverWidth(key);
							ub.setCoverHeight(map.get(key));
						}
					}
					
					String title = tempEls.first().attr("alt");
					// 中央戏剧学院校花林柯彤
					if(StringUtils.isNoneBlank(title)) {
						String[] names = title.split("校花");
						if(names.length > 1) {
							ub.setSchoolName(names[0]);
							ub.setName(names[1]);
						} else {
							ub.setName(names[0]);
						}
					}
					ub.setId(getMaxId());
					ubs.add(ub);
				}
			}
		}
		List<UnivBeauty> ubrss = new ArrayList<UnivBeauty>();
        for(UnivBeauty ub : ubs) {
        	UnivBeauty ubrs = getDetail(ub);
        	if(null != ubrs) {
        		ubrss.add(ubrs);
        	}
        }
        if(ubrss.size() > 0) {
        	insert2UbDb(ubrss);
        }
	}
	
	/**
	 * 获取列表数据
	 * @param url
	 * @throws FailingHttpStatusCodeException
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public UnivBeauty getDetail(UnivBeauty ub) {
		Document doc = null;
		try {
			doc = new GenericMyuall().getDoc(ub.getSourceUrl());
		} catch (Exception e) {
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setUrl(ub.getSourceUrl());
			ge.setType("2");
			ge.setContent(JSON.toJSONString(ub));
			ge.setDescription("获取Detail错误："+e.getMessage());
			insert2ErrDb(ge);
			return null;
		}
		Elements els = doc.select(".infodiv table tr");
		try {
			
		if(null != els && !els.isEmpty()) {
			for(Element el : els) {
				Elements tels = el.select("td");
				if(null != tels && tels.size() > 1) {
					if(tels.get(0).text().contains("龄")) {
						ub.setAge(tels.get(1).text());
					} else if(tels.get(0).text().contains("星")) {
						ub.setConstellation(tels.get(1).text());
					} else if(tels.get(0).text().contains("专")) {
						ub.setMajorName(tels.get(1).text());
					} else if(tels.get(0).text().contains("职")) {
						ub.setOccupation(tels.get(1).text());
					}
				}
			}
			
			// 获取内容：infocontent
			els = doc.select(".infocontent");
			if(null != els && !els.isEmpty()) {
				// 删除广告
				Elements tels = els.select("div.infocontent_ad");
				if(null != tels) tels.remove();
				// 删除尾部广告信息 林柯彤学生制服照http://www.xiaohuar.com/p-5-29.html
				if(els.first().html().contains("xiaohuar.com")) {
					tels = els.first().children();
					for(Element el : tels) {
						if(el.html().contains("xiaohuar.com")) el.remove();
					}
				}
				if(StringUtils.isNoneBlank(els.first().text().trim().replaceAll("&nbsp;", "").replaceAll(" ", ""))) ub.setDescription(els.first().html());
			}

			ub.setType(1);
			getImg(ub);
		} else {
			els = doc.select(".detail-id .content");
			if(null != els && !els.isEmpty()) {
				ub.setDescription(els.first().html());
			}
			ub.setType(2);
			getImg(doc, ub);
		}
		} catch (Exception e) {
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setUrl(ub.getSourceUrl());
			ge.setType("3");
			ge.setContent(JSON.toJSONString(ub));
			ge.setDescription("获取Detail错误："+e.getMessage());
			insert2ErrDb(ge);
			return null;
		}
		
		return ub;
	}
	


	public void getImg(Document doc, UnivBeauty ub) {
		Elements els = doc.select("#Middle-left-2 div.photo-Middle div table td a img ");
		try {
			if(null != els && !els.isEmpty()) {
				List<UnivBeautyImgs> ubis = new ArrayList<UnivBeautyImgs>();
				for(Element el : els) {
					UnivBeautyImgs ubi = new UnivBeautyImgs();
					ubi.setBeautyId(ub.getId());
					ubi.setBeautyName(ub.getName());
					ubi.setTitle(ub.getSchoolName() + "校花"+ub.getName());
					String src = el.attr("src");
					ubi.setUrl(src.startsWith("http")?src : baseUrl + src);
					ubis.add(ubi);
				}
				if(ubis.size() > 0 ) insert2UbiDb(ubis);
			}
		} catch (Exception e) {
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setUrl(ub.getSourceUrl());
			ge.setType("3");
			ge.setContent(JSON.toJSONString(ub));
			ge.setDescription("获取Detail错误："+e.getMessage());
			insert2ErrDb(ge);
			return;
		}
	}

	public void getImg(UnivBeauty ub) {
		Document doc = null;
		try {
			doc = new GenericMyuall().getDoc(ub.getSourceUrl().replace("p-", "s-"));
		} catch (Exception e) {
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setUrl(ub.getSourceUrl().replace("p-", "s-"));
			ge.setType("6");
			ge.setDescription("获取Img错误："+e.getMessage());
			insert2ErrDb(ge);
			return;
		}
		Elements els = doc.select(".ad-thumb-list li img");
		try {
			if(null != els && !els.isEmpty()) {
				List<UnivBeautyImgs> ubis = new ArrayList<UnivBeautyImgs>();
				for(Element el : els) {
					UnivBeautyImgs ubi = new UnivBeautyImgs();
					ubi.setBeautyId(ub.getId());
					ubi.setBeautyName(ub.getName());
					ubi.setTitle(ub.getSchoolName() + "校花"+ub.getName());
					String src = el.attr("src");
					ubi.setUrl(src.startsWith("http")?src : baseUrl + src);
					ubis.add(ubi);
				}
				if(ubis.size() > 0 ) insert2UbiDb(ubis);
			}
		} catch (Exception e) {
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setUrl(ub.getSourceUrl());
			ge.setType("3");
			ge.setContent(JSON.toJSONString(ub));
			ge.setDescription("获取Detail错误："+e.getMessage());
			insert2ErrDb(ge);
			return;
		}
	}
	
	public void insert2UbDb(List<UnivBeauty> ubs) {
		try {
			PreparedStatement ps = DbUtil.getConn().prepareStatement("insert into univ_beauty(description,cover,source_url,name,source,occupation,age,school_name,cover_width,type,cover_height,constellation,major_name,id) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
			for(UnivBeauty cc: ubs) {
				ps.setString(1, cc.getDescription());
				ps.setString(2, cc.getCover());
				ps.setString(3, cc.getSourceUrl());
				ps.setString(4, cc.getName());
				ps.setString(5, cc.getSource());
				ps.setString(6, cc.getOccupation());
				ps.setString(7, cc.getAge());
				ps.setString(8, cc.getSchoolName());
				ps.setInt(9, null == cc.getCoverWidth() ? 0 : cc.getCoverWidth());
				ps.setInt(10, cc.getType());
				ps.setInt(11, null == cc.getCoverHeight() ? 0 : cc.getCoverHeight());
				ps.setString(12, cc.getConstellation());
				ps.setString(13, cc.getMajorName());
				ps.setInt(14, cc.getId());
				
				ps.addBatch();
			}
			ps.executeBatch();
			
		} catch(Exception e) {
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setUrl("");
			ge.setType("5");
			ge.setContent(JSON.toJSONString(ubs));
			ge.setDescription("插入Detail错误："+e.getMessage());
			insert2ErrDb(ge);
		}
	}
	
	public void insert2UbiDb(List<UnivBeautyImgs> ubs) {
		try {
			PreparedStatement ps = DbUtil.getConn().prepareStatement("insert into univ_beauty_imgs(url,thumb_url,beauty_name,title,type_name,beauty_id) values(?,?,?,?,?,?)");
			for(UnivBeautyImgs cc: ubs) {
				ps.setString(1, cc.getUrl());
				ps.setString(2, cc.getThumbUrl());
				ps.setString(3, cc.getBeautyName());
				ps.setString(4, cc.getTitle());
				ps.setString(5, cc.getTypeName());
				ps.setInt(6, cc.getBeautyId());
				
				ps.addBatch();
			}
			ps.executeBatch();
			
		} catch(Exception e) {
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setUrl("");
			ge.setType("9");
			ge.setContent(JSON.toJSONString(ubs));
			ge.setDescription("插入Img错误："+e.getMessage());
			insert2ErrDb(ge);
		}
	}
	
	public void insert2ErrDb(GrapErr ge) {
		PreparedStatement grapPs = null;
		try {
			grapPs = DbUtil.getConn().prepareStatement("insert into grap_err(description,url,content,type) values(?,?,?,?)");
			grapPs.setString(1, ge.getDescription());
			grapPs.setString(2, ge.getUrl());
			grapPs.setString(3, ge.getContent());
			grapPs.setString(4, ge.getType());
			grapPs.execute();
			grapPs.close();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	private static Integer maxId = null;
	public int getMaxId() {
		if(maxId == null) {
			try {
				Connection connection = DbUtil.getConn();
				Statement statement = connection.createStatement();
				String sql = "select max(id) from univ_beauty";
				ResultSet rs = statement.executeQuery(sql);
				while(rs.next()) {
					maxId = rs.getInt(1) + 1;
				}
				rs.close();
				statement.close();
			} catch(Exception e) {
				e.printStackTrace();
			}
		} else {
			maxId++;
		}
		return maxId;
	}
	

	@Override
	public void run() {
		// TODO Auto-generated method stub
//		getLists();
	}
}
